﻿var pageSize = 15;
//预约列表
function loadList(page) {
    var accountName = $("#accountName").val();
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var productName = "";
    var url = api_domain + "/api/Store/GetAppointList?storeid=" + userid + "&pageSize=" + pageSize + "&pageNumber=" + page + "&accountName=" + accountName + "&productName=" + productName + "&startTime=" + startTime + "&endTime=" + endTime;
    if (!toLogin()) return;
    AjaxStart(url, "", function (data) {
        if (data.Data.Result == null || data.Data.Result.length < 1) {
            scroll_state = false;
            $(".orderlist").append("<div style='text-align:center;'>暂无数据</div>");
            return;
        }
        $(".orderlist").append(getItem(data.Data.Result));
        $(".box2").click(function () { toUrl(PageUrl.Appoint_Detail + "?aid=" + $(this).attr("data-aid")) })
        $(".box3").click(function () { show_update($(this).attr("data-aid"), $(this).attr("data-state")) })

        $(".btn_ok").click(function () { update_state($(this).parent().attr("data-aid"), 2) })
        $(".btn_no").click(function () { update_state($(this).parent().attr("data-aid"), 1) })
        $(".btn_close").click(function () { update_state($(this).parent().attr("data-aid"), 3) })
    });
}

function getItem(list) {
    var html = "";
    for (var i = 0; i < list.length; i++) {
        var item = list[i];
        var bg = item.Status == 2 ? "bg-hui" : ""

        html += "<div class='weui-panel weui-panel_access' style='margin-top:0px;'>"
        html += "<a class='weui-media-box weui-media-box_appmsg'>"
        html += "<div class='weui-media-box__bd box2' data-aid='" + item.AppointId + "'>"
        html += "<div class='weui-media-box weui-media-box_text' style='padding:0px;'>"
        html += "     <h4 class='weui-media-box__title' style='color:#333'>" + item.PName + "</h4>"
        html += "     <ul class='weui-media-box__info'><li class='weui-media-box__info__meta li-img'><img src='../../images/appoint/2.png'>" + item.RealName + "</li>"
        html += "     <li class='weui-media-box__info__meta li-img'><img src='../../images/appoint/1.png'>" + item.ApplyTime + "</li></ul>";
        html += " </div></div>"
        html += "<div class='weui-media-box__hd' style='margin-right:0px;'>"
        html += "<div class='box3 " + bg + "' data-aid='" + item.AppointId + "' data-state='" + item.Status + "'><div class='box4 state' style='border-bottom:solid 1px #e6e6e6;'>" + getState(item.Status) + "</div><div class='box4'>●●●</div>"
        html += " </div></div>"
        html += "</a>"
        html += "<div class='action-box hide' data-aid='" + item.AppointId + "'><a class='btn_ok'>确认</a> <a class='btn_no'>取消</a> <a class='btn_close'>关闭</a></div>"
        html += "</div>"
    }
    return html;
}

//查询
function search() {
    $(".orderlist").html("");
    loadList(1);
}


function loadDetail() {
    var aid = getQueryString("aid");
    var url = api_domain + "/api/Store/GetAppoint?storeid=" + userid + "&aid=" + aid;
    if (!toLogin()) return;
    AjaxStart(url, "", function (data) {
        if (!isSuccess(data)) return;
        var item = data.Data;
        if (item != null) {            
            $(".PName").html(item.PName)
            $(".ApplyTime").html(item.ApplyTime)
            $(".RealName").html(item.RealName)
            $(".Status").html(getState(item.Status))
            $(".CreatedTime").html(item.CreatedTime)
            $(".Phone").html(item.Phone)
            $(".Remark").html(item.Remark)
            $(".DiningTableTypeName").html(item.DiningTableTypeName);
            $(".DiningTableUserTotal").html(item.DiningTableUserTotal);
            //商品预约
            if (item.AppointType == 0) {
                $(".AppointType_0").removeClass("hide");
            }
            //餐位预约
            if (item.AppointType == 1) {
                $(".AppointType_1").removeClass("hide");
            }
        }
    });
}

//显示操作按钮
function show_update(aid, old_state) {
    $(".action-box").each(function () {
        if ($(this).attr("data-aid") == aid) {
            if ($(this).attr("class").indexOf("hide") > -1)
                $(this).removeClass("hide");
            else
                $(this).addClass("hide");
        }
        else
            $(this).addClass("hide");
    });
}

//隐藏操作按钮
function hide_update() {
    $(".action-box").addClass("hide");
}

//更新预约状态 0:待确认，1：已取消，2：已确认，3：已拒绝
function update_state(aid, state) {
    if (!toLogin()) return;
    hide_update()
    AjaxStart(api_domain + "/api/Store/UpdateAppointState", "aid=" + aid + "&state=" + state, function (data) {
        if (data.Code != 0) {
            Alert(data.Message)
            return;
        }
        Alert("操作成功");
        $(".box3").each(function () {
            if ($(this).attr("data-aid") == aid) {
                if (state == 2)
                    $(this).addClass("bg-hui");
                else
                    $(this).removeClass("bg-hui");
                $(this).find(".state").html(getState(state));
            }
        });
    });
}

function getState(state) {
    if (state == 0)
        return "待确认";
    else if (state == 1)
        return "已取消";
    else if (state == 2)
        return "已确认";
    else
        return "被拒绝";
}